package com.canbot.u05.activity.presidemode;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.canbot.u05.R;
import com.canbot.u05.socket.MsgSendUtils;
import com.canbot.u05.socket.MsgType;
import com.canbot.u05.utils.ToastTools;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author zmp
 * 主持模式选择动作界面
 */
public class ActionChoiceActivity extends Activity {

        private static final String TAG = "ActionChoiceActivity";

        List<ActionBean> mHandIDs = new ArrayList<>();

        @BindView(R.id.btn_back)
        Button btnBack;

        @BindView(R.id.btn_finish)
        Button btnFinish;

        @BindView(R.id.head_item)
        RelativeLayout headItem;

        @BindView(R.id.hand_item)
        RelativeLayout handItem;

        @BindView(R.id.leg_item)
        RelativeLayout legItem;

        @BindView(R.id.custem_item)
        RelativeLayout custemItem;

        @BindView(R.id.preside_action_lv)
        ListView presideActionLv;

        @BindView(R.id.textView_title)
        TextView textViewTitle;

        private ToastTools toastTools;

        private ListViewAdapter listViewAdapter;

        private int currentPosition;

        {
                mHandIDs.add(new ActionBean("10101", "述说", false));
                mHandIDs.add(new ActionBean("10104", "双手上下晃动", false));
                mHandIDs.add(new ActionBean("10107", "张开双臂", false));
                mHandIDs.add(new ActionBean("10109", "请看右边", false));
                mHandIDs.add(new ActionBean("10111", "请看左边", false));
                mHandIDs.add(new ActionBean("10110", "请看右上角", false));
                mHandIDs.add(new ActionBean("10112", "请看左上角", false));
                mHandIDs.add(new ActionBean("10113", "右手曲臂", false));
                mHandIDs.add(new ActionBean("10205", "敬礼", false));
                mHandIDs.add(new ActionBean("10228", "握手", false));
                mHandIDs.add(new ActionBean("10302", "打招呼", false));
                mHandIDs.add(new ActionBean("10502", "右手数数1", false));
                mHandIDs.add(new ActionBean("10503", "右手数数2", false));
                mHandIDs.add(new ActionBean("10504", "右手数数3", false));
                mHandIDs.add(new ActionBean("10505", "右手数数4", false));
                mHandIDs.add(new ActionBean("11701", "飞吻", false));
                mHandIDs.add(new ActionBean("12101", "摇头摆手", false));
                mHandIDs.add(new ActionBean("12301", "叉腰", false));
                mHandIDs.add(new ActionBean("10507", "耍帅", false));
                mHandIDs.add(new ActionBean("13101", "石头", false));
                mHandIDs.add(new ActionBean("13102", "剪刀", false));
                mHandIDs.add(new ActionBean("13103", "布", false));
        }

        List<ActionBean> mHeadIDs = new ArrayList<>();

        {
                mHeadIDs.add(new ActionBean("10201", "看右边", false));
                mHeadIDs.add(new ActionBean("10202", "看左边", false));
                mHeadIDs.add(new ActionBean("10203", "点头", false));
                mHeadIDs.add(new ActionBean("10204", "摇头", false));
                mHeadIDs.add(new ActionBean("14001", "抬头", false));
                mHeadIDs.add(new ActionBean("14002", "低头", false));
                mHeadIDs.add(new ActionBean("11501", "看左上角", false));
                mHeadIDs.add(new ActionBean("11801", "看右上角", false));
                mHeadIDs.add(new ActionBean("14010", "仰头大笑", false));
                mHeadIDs.add(new ActionBean("14008", "左顾右盼", false));
        }

        List<ActionBean> mLegIDs = new ArrayList<>();

        {
                mLegIDs.add(new ActionBean("10206", "前进", false));
                mLegIDs.add(new ActionBean("10207", "后退", false));
                mLegIDs.add(new ActionBean("10208", "左转", false));
                mLegIDs.add(new ActionBean("10208", "左转", false));
                mLegIDs.add(new ActionBean("10209", "右转", false));
                mLegIDs.add(new ActionBean("10211", "左转圈", false));
                mLegIDs.add(new ActionBean("10212", "右转圈", false));
        }

        private ActionBean currentActionBean;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_host_action_choice);
                ButterKnife.bind(this);
                toastTools = ToastTools.newInstance(this);
                Intent intent = getIntent();
                String actionID = intent.getStringExtra("actionID");
                String actionName = intent.getStringExtra("actionName");
                currentActionBean = getActionBeanByID(actionID);
                if (currentActionBean != null) {
                        currentActionBean.isChecked = true;
                }
                presideActionLv.setAdapter(listViewAdapter);
                presideActionLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                                if (currentActionBean != null) {
                                        currentActionBean.isChecked = false;
                                }
                                ActionBean actionBean = listViewAdapter.getList().get(position);
                                actionBean.isChecked = true;
                                currentActionBean = actionBean;
                                toastTools.showText(getString(R.string.action_preview));
                                MsgSendUtils.sendStringMsg(MsgType.ACTION, currentActionBean.actionID);
                                listViewAdapter.notifyDataSetChanged();
                        }
                });
                presideActionLv.setSelection(currentPosition - 1);
        }

        private ActionBean getActionBeanByID(String actionID) {
                if (TextUtils.isEmpty(actionID) || "-1".equals(actionID)) {
                        listViewAdapter = new ListViewAdapter(mHeadIDs);
                        return null;
                }
                for (int i = 0; i < mHeadIDs.size(); i++) {
                        ActionBean actionBean = mHeadIDs.get(i);
                        if (actionBean.actionID.equals(actionID)) {
                                listViewAdapter = new ListViewAdapter(mHeadIDs);
                                headItem.setSelected(true);
                                currentPosition = i;
                                return actionBean;
                        }
                }
                for (int i = 0; i < mLegIDs.size(); i++) {
                        ActionBean actionBean = mLegIDs.get(i);
                        if (actionBean.actionID.equals(actionID)) {
                                listViewAdapter = new ListViewAdapter(mLegIDs);
                                legItem.setSelected(true);
                                currentPosition = i;
                                return actionBean;
                        }
                }
                for (int i = 0; i < mHandIDs.size(); i++) {
                        ActionBean actionBean = mHandIDs.get(i);
                        if (actionBean.actionID.equals(actionID)) {
                                listViewAdapter = new ListViewAdapter(mHandIDs);
                                handItem.setSelected(true);
                                currentPosition = i;
                                return actionBean;
                        }
                }
                listViewAdapter = new ListViewAdapter(mHeadIDs);
                return null;
        }


        private void exit() {
                finish();
        }

        @OnClick({R.id.btn_back, R.id.btn_finish, R.id.head_item, R.id.hand_item, R.id.leg_item, R.id.custem_item})
        public void onViewClicked(View view) {
                switch (view.getId()) {
                        case R.id.btn_back:
                                finish();
                                break;
                        case R.id.btn_finish:
                                if (currentActionBean == null) {
                                        toastTools.showText(getString(R.string.please_select_action));
                                        return;
                                }
                                Intent intent = new Intent();
                                intent.putExtra("actionID", currentActionBean.actionID);
                                intent.putExtra("actionName", currentActionBean.actionName);
                                setResult(RESULT_OK, intent);
                                finish();
                                break;
                        case R.id.head_item:
                                if (headItem.isSelected()) {
                                        return;
                                }
                                headItem.setSelected(true);
                                handItem.setSelected(false);
                                legItem.setSelected(false);
                                listViewAdapter.setList(mHeadIDs);
                                break;
                        case R.id.hand_item:
                                if (handItem.isSelected()) {
                                        return;
                                }
                                headItem.setSelected(false);
                                handItem.setSelected(true);
                                legItem.setSelected(false);
                                listViewAdapter.setList(mHandIDs);
                                break;
                        case R.id.leg_item:
                                if (legItem.isSelected()) {
                                        return;
                                }
                                headItem.setSelected(false);
                                handItem.setSelected(false);
                                legItem.setSelected(true);
                                listViewAdapter.setList(mLegIDs);
                                break;
                        case R.id.custem_item:
                                toastTools.showText(getString(R.string.not_available));
                                break;
                }
        }


        class ListViewAdapter extends BaseAdapter {

                public List<ActionBean> getList() {
                        return list;
                }

                private List<ActionBean> list;

                private LayoutInflater inflater;

                public ListViewAdapter(List<ActionBean> list) {
                        this.list = list;
                        inflater = LayoutInflater.from(ActionChoiceActivity.this);
                }

                @Override
                public int getCount() {
                        return list == null ? 0 : list.size();
                }

                public void setList(List<ActionBean> list) {
                        this.list = list;
                        notifyDataSetChanged();
                }

                @Override
                public Object getItem(int arg0) {
                        return arg0;
                }

                @Override
                public long getItemId(int arg0) {
                        return arg0;
                }

                @Override
                public View getView(int position, View convertView, ViewGroup arg2) {
                        ViewHolder viewHolder;
                        if (convertView == null) {
                                convertView = inflater.inflate(R.layout.compere_list_item, null);
                                viewHolder = new ViewHolder();
                                viewHolder.title = (TextView) convertView.findViewById(R.id.text_name);
                                viewHolder.imageView = (ImageView) convertView.findViewById(R.id.cbox_host_mode_choose_exist_audio);
                                convertView.setTag(viewHolder);
                        } else {
                                viewHolder = (ViewHolder) convertView.getTag();
                        }
                        viewHolder.title.setText(list.get(position).actionName);
                        viewHolder.imageView.setVisibility(list.get(position).isChecked ? View.VISIBLE : View.INVISIBLE);
                        return convertView;
                }

                private class ViewHolder {

                        public TextView title;

                        public ImageView imageView;
                }

        }

        class ActionBean {

                private String actionID;

                private String actionName;

                private boolean isChecked;

                public ActionBean(String actionID, String actionName, boolean isChecked) {
                        this.actionID = actionID;
                        this.actionName = actionName;
                        this.isChecked = isChecked;
                }
        }
}
